package com.sunlong.jiangwei.web;

import com.sunlong.jiangwei.entity.JwUser;
import com.sunlong.jiangwei.entity.result.Result;
import com.sunlong.jiangwei.service.IUserService;
import com.sunlong.jiangwei.util.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/jiangwei/user")
@Slf4j
public class UserController {

    @Autowired
    private IUserService userService;

    @PostMapping("/login")
    public Result<Boolean> login(@RequestBody Map<String,Object> map){
        Map<String,String> errorMap = userService.login(map);
        if (errorMap.containsKey("errorMsg")){
            return ResultUtil.error(errorMap.get("errorMsg"));
        }
        if (errorMap.get("login").equals("yes")){
            return ResultUtil.success(errorMap.get("user"));
        }else {
            return ResultUtil.error("用户密码错误");
        }
    }

    @PostMapping("/register")
    public Result<Boolean> register(@RequestBody Map<String,Object> map){
        Map<String,String> errorMap = userService.register(map);
        if (errorMap.containsKey("errorMsg")){
            return ResultUtil.error(errorMap.get("errorMsg"));
        }
        return ResultUtil.success("注册成功！");
    }

    @PostMapping("/changePassword")
    public Result<Boolean> changePassword(@RequestBody Map<String,Object> map){
        Map<String,String> errorMap = userService.changePassword(map);
        if (null != errorMap && errorMap.containsKey("errorMsg")){
            return ResultUtil.error(errorMap.get("errorMsg"));
        }
        return ResultUtil.success();
    }

    @PostMapping("/list")
    public Result<List<JwUser>> listResult(@RequestBody Map<String,Object> map){
        List<JwUser> users = userService.findAllAdminUser(map);
        return ResultUtil.data(users);
    }

    @PostMapping("/add")
    public Result<Void> add(@RequestBody Map<String,Object> map){
        Map<String,String> errorMap = userService.add(map);
        if (errorMap.containsKey("errorMsg")){
            return ResultUtil.error(errorMap.get("errorMsg"));
        }
        return ResultUtil.success();
    }

    @PostMapping("/delete")
    public Result<Void> delete(@RequestBody Map<String,Object> map){
        Map<String,String> errorMap = userService.delete(map);
        return ResultUtil.success();
    }

    @PostMapping("/userRole/modify")
    public Result<Void> userRoleModify(@RequestBody Map<String,Object> map){
        Map<String,String> errorMap = userService.userRoleModify(map);
        return ResultUtil.success();
    }

    @PostMapping("/userRole/delete")
    public Result<Void> userRoleDelete(@RequestBody Map<String,Object> map){
        Map<String,String> errorMap = userService.userRoleDelete(map);
        return ResultUtil.success();
    }

    @GetMapping("/userRole/list")
    public Result<List<JwUser>> userRoleList(){
        List<JwUser> users = userService.userRoleList();
        return ResultUtil.data(users);
    }
}
